package com.frank.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.discussion.Discussion;
import com.frank.model.entity.discussion.DiscussionReport;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.vo.DiscussionReportVO;
import com.frank.oj.service.admin.discussion.AdminDiscussionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/7
 */
@RestController
@RequestMapping("/api/admin")
@RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
@RequiresAuthentication
public class AdminDiscussionController {

    @Resource
    private AdminDiscussionService adminDiscussionService;

    @PutMapping("/discussion")
    public CommonResult<Void> updateDiscussion(@RequestBody Discussion discussion) {
        try {
            adminDiscussionService.updateDiscussion(discussion);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }


    @DeleteMapping("/discussion")
    public CommonResult<Void> deleteDiscussion(@RequestBody List<Integer> didList) {
        try {
            adminDiscussionService.deleteDiscussion(didList);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }


    @GetMapping("/discussion-report")
    public CommonResult<IPage<DiscussionReportVO>> getDiscussionReport(@RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                                                       @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage) {
        return CommonResult.successResponse(adminDiscussionService.getDiscussionReport(currentPage, limit));
    }

    /**
     * 处理举报
     *
     * @param discussionReport 举报信息
     * @return null
     */
    @PutMapping("/discussion-report")
    public CommonResult<Void> updateDiscussionReport(@RequestBody DiscussionReport discussionReport) {

        try {
            adminDiscussionService.updateDiscussionReport(discussionReport);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }
}
